#include "instruction.h"

Instruction::Instruction(){}

Instruction::Instruction(int type , double *coords , double angle , double *transform){
    this->type = type;
    this->angle = angle;

    if(transform != 0){
        for(int i = 0 ; i < 16 ; i++){
            this->transform[i] = transform[i];
        }
    }
    else{
        for(int i = 0 ; i < 16 ; i++){
            if(i % 5 == 0){
                this->transform[i] = 1.0;
            }
            else{
                this->transform[i] = 0.0;
            }
        }
    }

    if(coords != 0){
        this->coords[0] = *coords;
        this->coords[1] = *(coords + 1);
        this->coords[2] = *(coords + 2);
    }
    else{
        this->coords[0] = this->coords[1] = this->coords[2] = 0.0;
    }
}

void Instruction::execute(){
    switch(type){
        case Instruction::ROTATION:{
            glRotated(angle , coords[0] , coords[1] , coords[2]);
            break;
        }
        case Instruction::SCALING:{
            glScaled(coords[0] , coords[1] , coords[2]);
            break;
        }
        case Instruction::TRANSLATION:{
            glTranslated(coords[0] , coords[1] , coords[2]);
            break;
        }
        case Instruction::GENERIC_TRANSFORMATION:{
            glMultMatrixd(transform);
            break;
        }
    }
}

void Instruction::show(){
    printf("type = %d, angle = %lf, coords = (%lf , %lf , %lf)\nTransform (row-major)\n:" , type , angle , coords[0] , coords[1] , coords[2]);

    for(int i = 0 ; i < 4 ; i++){
        for(int j = 0 ; j < 4 ; j++){
            printf("%15lf " , transform[(j * 4) + i]);
        }
        printf("\n");
    }
    printf("\n");
}
